import java.util.Stack;

public class Test02 {
    public String decodeString(String s) {
        Stack<String> stackStr = new Stack<>();
        Stack<Integer> stackNum = new Stack<>();
        StringBuilder currentStr = new StringBuilder();
        int k = 0;

        for (char c : s.toCharArray()) {
            if (Character.isDigit(c)) {
                // 如果是数字，计算数字
                k = k * 10 + (c - '0');
            } else if (c == '[') {
                // 遇到 '['，将当前数字和当前字符串压入栈
                stackNum.push(k);
                stackStr.push(currentStr.toString());
                // 重置当前字符串和数字
                currentStr = new StringBuilder();
                k = 0;
            } else if (c == ']') {
                // 遇到 ']'，将当前字符串和栈顶的重复次数结合
                StringBuilder decodedStr = new StringBuilder(stackStr.pop());
                int repeatTimes = stackNum.pop();
                for (int i = 0; i < repeatTimes; i++) {
                    decodedStr.append(currentStr);
                }
                currentStr = decodedStr;
            } else {
                // 普通字符，直接加入当前字符串
                currentStr.append(c);
            }
        }

        return currentStr.toString();
    }
}
